package MapPack;

import EntityPack.Entity;
import com.badlogic.gdx.math.Vector2;

import java.util.ArrayList;

/**
 * Created by Aaron on 8/19/2014.
 */
public class Chunk {
    private final Map map;
    private final Vector2 position;
    private final int size;
    private final Cell[][] cells;

    public Chunk (Map map, Vector2 position, int size) {
        this.map = map;
        this.position = position;
        this.size = size;
        this.cells = new Cell[size][size];

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                this.cells[i][j] = new Cell(this.map, ((int) position.x) * size + i, ((int) position.y) * size + j, 0);
            }
        }
    }

    public int getSize ( ) {
        return this.size;
    }

    public Cell getCell(int x, int y) {

        Cell result;

        try {
            result = this.cells[x][y];
        } catch (ArrayIndexOutOfBoundsException e) {
            result = null;
        }

        return result;
    }

    public ArrayList<Entity> getEntities ( ) {
        ArrayList<Entity> entities = new ArrayList<>();
        for (Cell[] cellRow : this.cells) {
            for (Cell cell : cellRow)
                entities.addAll(cell.getEntities());
        }

        return entities;
    }

    public Vector2 getPosition ( ) {
        return this.position;
    }
}
